package com.litchi.activity;

import java.util.ArrayList;

import com.amap.api.maps2d.AMap;
import com.amap.api.maps2d.MapView;
import com.amap.api.maps2d.model.BitmapDescriptorFactory;
import com.amap.api.maps2d.model.LatLng;
import com.amap.api.maps2d.model.Marker;
import com.amap.api.maps2d.model.MarkerOptions;
import com.amap.api.services.cloud.CloudItem;
import com.amap.api.services.cloud.CloudItemDetail;
import com.amap.api.services.cloud.CloudResult;
import com.amap.api.services.cloud.CloudSearch;
import com.amap.api.services.cloud.CloudSearch.OnCloudSearchListener;
import com.amap.api.services.cloud.CloudSearch.SearchBound;
import com.amap.api.services.core.AMapException;
import com.litchi.R;
import com.litchi.app.MyApplication;
import com.litchi.app.SysApplication;

import android.app.Activity;
import android.app.Application;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.widget.ImageView;

public class AroundActivity extends Activity implements OnCloudSearchListener{
	private MapView mapView;
	private CloudSearch.Query mQuery;
	private AMap aMap;
	private MyApplication application;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		application=(MyApplication)getApplication();
		setContentView(R.layout.activity_around);
		mapView=(MapView)findViewById(R.id.map);
		mapView.onCreate(savedInstanceState);// 此方法必须重写
		aMap = mapView.getMap();
		aMap.setTrafficEnabled(true);// 显示实时交通状况
		//地图模式可选类型：MAP_TYPE_NORMAL,MAP_TYPE_SATELLITE,MAP_TYPE_NIGHT
		aMap.setMapType(AMap.MAP_TYPE_NORMAL); 
		
	}
	
	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		
		CloudSearch	mCloudSearch = new CloudSearch(this);// 初始化查询类
		mCloudSearch.setOnCloudSearchListener(this);// 设置回调函数
		SearchBound bound = new SearchBound("杭州");// 输入city “全国”，为本表全部搜索。
		
		try {
			mQuery = new CloudSearch.Query("571b29c97bbf197be325605d", "丝绸", bound);
		} catch (AMapException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		mCloudSearch.searchCloudAsyn(mQuery);
		 
	}
	
	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
	}
	
	public void initView(){
		
	}

	@Override
	public void onCloudItemDetailSearched(CloudItemDetail arg0, int arg1) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onCloudSearched(CloudResult arg0, int arg1) {
		// TODO Auto-generated method stub
		ArrayList<CloudItem> clouds=arg0.getClouds();
		 
		for (CloudItem item:clouds) {
			LatLng mLatLng=new LatLng(item.getLatLonPoint().getLatitude(),item.getLatLonPoint().getLongitude());
			ImageView imageView=new ImageView(this);
			imageView.setDrawingCacheEnabled(true);
			application.imgLoader.displayImage(item.getCloudImage().get(0).getUrl(), imageView);
			//绘制marker
			Marker marker = aMap.addMarker(new MarkerOptions()
			        .position(mLatLng)
			        .icon(BitmapDescriptorFactory.fromBitmap(imageView.getDrawingCache()))
					.draggable(true));
			
		}
		
	}
}
